package com.lwp.test.spring.springlistener;

import cn.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 监听类实例化后
 */
@Component
//@Lazy
public class MyBeanPostProcessor implements BeanPostProcessor {


    /**
     * 一般使用这个。 afterPropertiesSet 和 init-method 之间。实例变量已经执行
     * 	 Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
     * 	 initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
     * 	 or a custom init-method). The bean will already be populated with property values.
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(" ****** MyBeanPostProcessor postProcessBeforeInitialization ****** ");
//        if (bean.getClass().isAnnotationPresent())
//        System.out.println("********** MyBeanPostProcessor invoke postProcessBeforeInitialization start");
//        if (bean instanceof Test) {
//            Test bean1 = (Test) bean;
//            bean1.hello();
//        }
//        System.out.println("********** MyBeanPostProcessor invoke postProcessBeforeInitialization end");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(" ****** MyBeanPostProcessor postProcessAfterInitialization ****** ");
        return bean;
    }
}
